import React, { Component } from 'react'
import  {createIncrementAction,createDecrementAction,createIncrementAsyncAction} from '../../redux/count_action'
// 引入connect 用于连接UI组件和redux
import {connect} from 'react-redux'
// UI组件

// 定义UI组件
 class Count extends Component {
  increment =()=>{
    const value  = this.selectNum.value
    this.props.jia(value*1)
  }
  decrement =()=>{
    const value  = this.selectNum.value
    this.props.jian(value*1)
  }
  incrementIfOdd =()=>{
    const value  = this.selectNum.value
    if(this.props.count%2!==0){
      this.props.jia(value*1)
    }
   
  }
  incrementAsync =()=>{
    const value  = this.selectNum.value
    this.props.jiaAsync(value*1,500)
  }
  render() {
    console.log('ui组件接收的props:',this.props)//此时接收到方法+状态+store
    return (
      <div>
        <h1>当前求和为：{this.props.count}</h1>
        <select ref={c=>this.selectNum=c}>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>
        &nbsp;
        <button onClick={this.increment}>+</button> &nbsp;
        <button onClick={this.decrement}>-</button> &nbsp;
        <button onClick={this.incrementIfOdd}>当前和为奇数+</button> &nbsp;
        <button onClick={this.incrementAsync}>异步+</button>
      </div>
    )
  }
}

// 创建并暴露一个Count的容器组件
export default connect(
  state=>({count:state}),
  {
    jia:createIncrementAction,
    jian:createDecrementAction,
    jiaAsync:createIncrementAsyncAction
  }

)(Count)